<?php
/*****************************************************************
	Std PHP Framework
	Module DB MySQL 
	$Id: mod_mysql.php 4 2008-02-25 23:18:23Z RusAnon $
*****************************************************************/
/*****************************************************************
	Module DB MySQL Configuration
*****************************************************************/
/*
$db_port='3306';
$db_addr='127.0.0.1';
$db_user='';
$db_pass='';
$db_base='';
*/
function db_connect()
{
	global $db_con,$db_port,$db_addr,$db_user,$db_pass,$db_base;
	$result = mysql_connect("$db_addr:$db_port",$db_user,$db_pass);
	if (!$result)
	{
		//echo 'cant connect';
		return false;
	}
   	else
		$db_con=$result;
	if (!mysql_select_db($db_base))
		return false;
	define("ModMYSQL_Connected",		1);
	return $result;
}
function db_disconnect()
{
	global $db_con;
	mysql_close($db_con);
}
function db_err($sql="",$err="")
{
	$mysql_err=mysql_error();

	echo 'MySQL ERROR '.$err.' <BR> :'.$mysql_err.' <BR> ATTEMPTED QUERY WAS :'.$sql;

	die();
}
function db_in($arr,$q="'",$k=1)
{
	$f=0;
	$d="";
	$r="";
	if (is_array($arr))
	foreach ($arr as $key => $val)
	{
		if ($k)
		{
			$r.="$d$q$key$q";
		}
		else
		{
			$r.="$d$q$val$q";
		}
		if(!$f)
		{
			$f=1;
			$d=" , ";
		}
	}
	else
	$r="'$arr'";
	return $r;
}
function db_query($sql,$func="")
{
	$res=mysql_query($sql);
	if (!$res)
	{
		db_err($sql,$func);
	}
	else
	{
		if (strpos(strtolower(substr($sql,0,6)),"select") !== false)
		{
			$n=mysql_num_rows($res);
			$f=mysql_num_fields($res);
		}
		else
		{
			$n=0;
			$f=0;
		}
		return array(
			"res"	=> $res,
			"n"	=> $n,
			"f"	=> $f,
			"a"	=> mysql_affected_rows(),
			"id"	=> mysql_insert_id(),
		);
	}

}
function db_select($sql)
{
	$res=mysql_query($sql);
	if (!$res)
	{
		db_err($sql);
	}
	else
	{
		$n=mysql_num_rows($res);
		$f=mysql_num_fields($res);
		if ($n)
			return array(
				"res"	=> $res,
				"n"	=> $n,
				"f"	=> $f,
			);
		else
			return 0;
	}
}
function db_select_row($sql)
{
	$res=mysql_query($sql);
	if (!$res)
	{
		db_err($sql);
	}
	else
	{
		$n=mysql_num_rows($res);
		if ($n)
			return mysql_fetch_assoc($res);
		else
			return 0;
	}
}
function db_row(&$res)
{
	return mysql_fetch_array($res['res']);
}
function db_table(&$res,$key="0")
{
	$r=array();
	for ($i = 0;$i < $res['n'];$i++)
	{
		$row=db_row($res);
		foreach ($row as $k => $val)
		{
			$r[$row[$key]][$k]=$val;
		}
	}
	return $r;
}
function db_update($table,$row,$where="")
{
	$sql="UPDATE $table SET ";
	$f=0;
	foreach ($row as $k => $v)
	{
		if (!$f)
			$f=1;
		else
			$sql.=",";
		$sql.="$k='$v'";
	}
	if ($where)
	$sql.=" WHERE ".$where;
	return db_query($sql);
}
function db_insert($table,$row)
{
	$sql="INSERT INTO $table (";
	$f=0;
	$vals="";
	foreach ($row as $k => $v)
	{
		if (!$f)
			$f=1;
		else
		{
			$sql.=",";
			$vals.=",";
		}
		$sql.="$k";
		$vals.="'$v'";
	}
	$sql.=") VALUES ($vals)";
	return db_query($sql);
}
function db_count($table,$where="")
{
	$r = db_query("SELECT count(*) as __qty__ FROM $table ".($where?"Where $where":""));
	$row = db_row($r);
	return $row['__qty__'];
}
function db_InsertOrUpdate ($table,$row,$where="")
{
	if (db_count($table,$where))
	{
		return db_update($table,$row,$where);
	}
	else
	{
		return db_insert($table,$row);
	}
}
?>